package com.xyebank.ledger.service.zy.adapter;

import org.apache.commons.lang3.StringUtils;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.springframework.stereotype.Component;

import com.xyebank.ledger.db.entitys.LRepaymentInf;
import com.xyebank.ledger.service.common.conveter.RePayBeanConverter;
import com.xyebank.ledger.service.constant.CommonConstant;
import com.xyebank.ledger.service.constant.ProductConstant;
import com.xyebank.ledger.util.IDUtil;
import com.xyebank.ledger.vo.deal.ZYCashRepaymentInfVo;

@Component(value = "zyRepaymentBeanConverter")
public class ZYRepaymentBeanConverter implements RePayBeanConverter<ZYCashRepaymentInfVo> {

    ModelMapper             modelMapper   = new ModelMapper();

    class ZYRepaymentPropertyMap extends PropertyMap<ZYCashRepaymentInfVo, LRepaymentInf> {
        protected void configure() {
            map(source.getMblNo(), destination.getMblNo());
            map(source.getOrderNo(), destination.getOrgOrderNo());
            map(source.getRepayTime(), destination.getRepayTime());
            map(source.getRepayAmount(), destination.getRepayAmount());

        }
    }



    public ZYRepaymentBeanConverter() {
        modelMapper.addMappings(new ZYRepaymentPropertyMap());
    }


    
    // 中邮还款无状态映射
    @Override
    public LRepaymentInf covert(ZYCashRepaymentInfVo x) {
        LRepaymentInf entity = modelMapper.map(x, LRepaymentInf.class);
        entity.setId(IDUtil.generateId(x.getId().toString(), "zy"));
        buildDataSource(x, entity);

        entity.setXyOrgNo(ProductConstant.ZY_LOAN_ORG);
        entity.setXyProductNo(ProductConstant.ZY_LOAN_PROD_ID);
        return entity;
    }
    
    

    private void buildDataSource(ZYCashRepaymentInfVo x, LRepaymentInf entity) {
        String loanNo = x.getLoanNo();
        String str = loanNo.substring(5, 6);
        if(StringUtils.equals(str, "4")){
            entity.setDataSource(CommonConstant.SJD_STR);
            
        } else if(StringUtils.equals(str, "3")){
            entity.setDataSource(CommonConstant.JRY_STR);
            
        } else{
            entity.setDataSource(CommonConstant.UNKOWN_STR);
            
        }
    }
}
